package com.github.admin.edu.security.jwt.server.service.impl;

import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.oauth.server.api.entity.TUserInformation;
import com.github.admin.edu.security.jwt.server.domain.ServerRoleManagerDomain;
import com.github.admin.edu.security.jwt.server.domain.ServerUserRoleManagerDomain;
import com.github.admin.edu.security.jwt.server.domain.ServerUserSysLoginDomain;
import com.github.admin.edu.security.jwt.server.entity.TSysRole;
import com.github.admin.edu.security.jwt.server.entity.TSysRoleUser;
import com.github.admin.edu.security.jwt.server.entity.TSysUser;
import com.github.admin.edu.security.jwt.server.service.ServerUserInformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-30
 */
@Service
@Slf4j
public class ServerUserInformationServiceImpl implements ServerUserInformationService {

    @Autowired
    private ServerUserSysLoginDomain sysLoginDomain;

    @Autowired
    private ServerUserRoleManagerDomain roleManagerDomain;

    @Autowired
    private ServerRoleManagerDomain managerDomain;

    /**
     * 根据用户登录账号，查询用户角色，组织等相关信息
     *
     * @param userId 用户登录账号
     * @return 用户信息详情
     */
    @Override
    public String getUserRoleOrgId(String userId) {
        JsonEntity<TUserInformation> jsonEntity = new JsonEntity<>();
        if (StringUtils.isNotBlank(userId)) {
            List<TSysUser> list = sysLoginDomain.getByUserid(userId);
            if (null != list && list.size() > 0) {
                TUserInformation userInformation = new TUserInformation();
                TSysUser user = list.get(0);
                if (null != user) {
                    userInformation.setId(user.getId());
                    userInformation.setName(user.getName());
                    userInformation.setOrgIds(String.valueOf(user.getOrgid()));
                    userInformation.setOrgNames(user.getOrgname());
                    List<TSysRoleUser> roleUsers = roleManagerDomain.getAllByUserid(user.getId());
                    if (null != roleUsers && roleUsers.size() > 0) {
                        List<Long> roleIds = new ArrayList<>();
                        roleUsers.parallelStream().forEach(tSysRoleUser -> {
                            roleIds.add((long) tSysRoleUser.getRoleid());
                        });
                        if (null != roleIds && roleIds.size() > 0) {
                            List<TSysRole> sysRoles = managerDomain.findAllById(roleIds);
                            String roleId = "", roleName = "";
                            if (null != sysRoles && roleIds.size() > 0) {
                                for (TSysRole sysRole : sysRoles) {
                                    if (StringUtils.isBlank(roleId)) {
                                        roleId = "" + sysRole.getId();
                                        roleName = sysRole.getName();
                                    } else {
                                        roleId = roleId + "," + sysRole.getId();
                                        roleName = roleName + "," + sysRole.getName();
                                    }
                                }
                            }
                            userInformation.setOrgNames(roleName);
                            userInformation.setOrgIds(roleId);
                        }
                    }
                    jsonEntity.setData(userInformation);
                }
            }
        }
        return JsonUtils.toJson(jsonEntity);
    }
}
